<?
//require_once("conf.inc.php");
//require_once("dbaccess.php");	
//$enc = "UTF-8";
include_once("../Dao/db.php");
$idx = 0;
$line = "";	



/*
$con = dbconnect();
$sql = "SET NAMES utf8";
doSQL($sql);
*/

//$sql = "SELECT distinct(carib_tag) FROM tag_mst ORDER BY carib_tag asc";
//$result = doSQL($sql);

//20071112～20081212まで

//while($row = mysql_fetch_array($result)){

	$enc = "";

for($year=2007;$year<=2008;$year++){
	$mon_tmp = $year==2007 ? 11 : 1;
	for($month=$mon_tmp;$month<=12;$month++){
		$day_tmp = $year==2007 && $month==11 ? 12 : 1;
		for($day = $day_tmp; checkdate($month, $day, $year); $day++ ){

			$date = sprintf("%04d%02d%02d",$year,$month,$day);
			$date_mysql = sprintf("%04d-%02d-%02d",$year,$month,$day);
			echo "date:$date<br/>";

			$url2 = "http://searchranking.yahoo.co.jp/burst_ranking/$date";
			echo "<h1>url:$url2</h1>";
		
			$fp2 = fopen($url2,'r');
		
			if($fp2){
				$line2 = $tmp_line2 = "";
				while(!feof($fp2)){
			
					//ファイルから一行読み込む
					$line2 = fgets($fp2);
					//スペースをまとめる
					$line2 = preg_replace('/\s{2,}/', "", $line2);
					$tmp_line2 .= trim($line2);
																																	
				}		
		
				$line2 = $tmp_line2;
				//echo htmlspecialchars($line2);
				//echo "<!--".$line."//-->";
		
				$matches3 = array();
				if(preg_match_all("/<li id=\"rank_([0-9]+)\" class=\"pkg\">.{5,30}<a href=\"[^>]{5,400}?\">([^<]{5,100})<\/a>.{5,300}<p>([^>]{5,100})<\/p><\/div><p class=\"rankPoint\">([^<]{1,50})<span>/i",$line2, $matches3)){
					//print_r($matches3);
					if($matches3[2][0]!="ネットde家系図"){
						for($j=0;$j<count($matches3[1]);$j++){
							$rank = (int)$matches3[1][$j];
							$name = $matches3[2][$j];
							$comment = strip_tags($matches3[3][$j]);
							$kanma = ",";
							$point = str_replace($kanma,"",$matches3[4][$j]);
			
							$sql = "INSERT INTO DailyKeywordRakning(date,rank,keywordName,note,point) VALUES('$date_mysql',$rank,'$name','$comment',$point)";
							$result = mysql_query($sql);
							echo $sql;
						}
					}
				}
			}

		}//day
//exit;
	}//month
}//year

//csv_out($csv,$down_file);
//dbclose($con);

function surround_dq($word){
	return "'".$word."'";
}


function format_as_row($words=array()){
	$words_dq = array_map('surround_dq',$words);
	$row = implode(",",$words_dq)."\r\n";
	return $row;
}


function csv_out($csv,$down_file){
	// CSVの出力
	header ("Content-Type: application/force-download");
	header ("Content-Disposition: attachment; filename=" . $down_file);
	header ("Content-Description: File Transfer");
	header ("Content-Length: " . strlen(join("",$csv)) );
	foreach ($csv as $mval) {
		//echo $mval;
		echo mb_convert_encoding($mval, "Shift_JIS", "EUC-JP");
		flush();
		ob_flush();
		usleep(10000); 
	}
	exit;
}

?>
